<?php

declare(strict_types=1);

namespace app\controller\Request;

use app\controller\Response\TradeAlipayRefundResponse;
use app\controller\Util\Signer;

class TradePolymerizePayRequest extends AbstractRequest
{
    /**
     * @throws \Exception
     */
    public function getData(): array
    {
        $params = [
            'mch_no' => $this->getMchno(),
            'method' => 'trade.polymerize.pay',   // 聚合支付接口名称
            'charset' => 'utf-8',
            'sign_type' => 'RSA',
            'timestamp' => date('Y-m-d H:i:s'),
            'version' => '1.0',
            'notify_url' => $this->getNotifyUrl(),
            'return_url' => $this->getReturnUrl(),
            'biz_content' => $this->getBizContent()
        ];

        $signer = new Signer();
        $signer->setParams($params);
        $signer->setKey($this->getAppPrivateKey());
        $params['sign'] = $signer->sign();

        return $params;
    }

    public function sendData($data): TradeAlipayRefundResponse
    {
        $headers = [
            'Content-Type' => 'application/json'
        ];
        $response = $this->httpClient->request('POST', $this->endpoint, $headers, json_encode($data));

        return $this->response = new TradeAlipayRefundResponse($this, json_decode($response->getBody()->getContents(), true));
    }
}
